package order;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import db.DBManager;

public class Order {
	private String number;
	private String customer;
	private Timestamp date;

	public static List<Order> find_all() {
		List<Order> list = new ArrayList<Order>();

		Connection con = null;
		Statement stm = null;
		ResultSet rs = null;

		String sql = "select * from  orders order by id desc";
		try {
			con = DBManager.getConnection();
			stm = con.createStatement();
			rs = stm.executeQuery(sql);
			while (rs.next()) {
				Order order = new Order();
				order.setNumber(String.format("%04d", rs.getInt("id")));
				order.setCustomer(rs.getString("customer"));
				order.setDate(rs.getTimestamp("date"));

				list.add(order);
			}
			rs.close();
		}
		catch (SQLException e) {
			rs = null;
			e.printStackTrace();
		}
		finally {
			if (stm != null) {
				try {stm.close();}
				catch (SQLException e) {e.printStackTrace();}
			}
			if (con != null) {
				try {con.close();}
				catch (SQLException e) {e.printStackTrace();}
			}
		}
		return list;
	}
	/**
	 * @return number
	 */
	public String getNumber() {
		return number;
	}
	/**
	 * @param number セットする number
	 */
	public void setNumber(String number) {
		this.number = number;
	}
	/**
	 * @return customer
	 */
	public String getCustomer() {
		return customer;
	}
	/**
	 * @param customer セットする customer
	 */
	public void setCustomer(String customer) {
		this.customer = customer;
	}
	/**
	 * @return date
	 */
	public Timestamp getDate() {
		return date;
	}
	/**
	 * @param date セットする date
	 */
	public void setDate(Timestamp date) {
		this.date = date;
	}
}
